package net.projecteuler.prob;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Problem81 implements Runnable {

	private int[][] matrix = null;
	private int[][] scoreMat = null;
	
	private static String FILE_PATH = "./res/matrix.txt";
	private static int SIZE = 80;
	
	@Override
	public void run() {
		readMatrix();
		
		scoreMat = new int[SIZE][SIZE];
		
		for(int i = 0; i < SIZE; i++){
			for(int j = 0; j < SIZE; j++){
				if(i == 0 && j == 0)
					scoreMat[i][j] = matrix[i][j];
				else if(i == 0)
					scoreMat[i][j] = scoreMat[i][j-1] + matrix[i][j];
				else if(j == 0)
					scoreMat[i][j] = scoreMat[i-1][j] + matrix[i][j];
				else
					scoreMat[i][j] = scoreMat[i - 1][j] > scoreMat[i][j - 1] ? scoreMat[i][j - 1] + matrix[i][j] : scoreMat[i - 1][j] + matrix[i][j];
			}
		}
		
		System.out.println(scoreMat[SIZE - 1][SIZE - 1]);
		
	}
	
	private void readMatrix(){
		try {
			File file = new File(FILE_PATH);
			BufferedReader br = new BufferedReader(new FileReader(file));
			
			matrix = new int[SIZE][SIZE];
			
			for(int i = 0; i < SIZE; i++){
				String line = br.readLine();
				String[] strs = line.split(",");
				
//				matrix[i] = new int[SIZE];
				for(int j = 0; j < SIZE; j++)
					matrix[i][j] = Integer.parseInt(strs[j]);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
	}

}
